//
//  StorageManager.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/2/14.
//

import UIKit
import YYCache

class CacheManager: NSObject {
    
    static let manager = CacheManager()
    /**
     缓存name
     */
    let cacheManage =  YYCache(name: "R")
    
    /**
     * 存
     */
    @objc public func setValueForKey(_ key: String, _ value: NSCoding) {
        guard let cache = self.cacheManage else {
#if DEBUG
            print("保存失败!, 初始化失败")
#endif
            return
        }
        cache.setObject(value, forKey: key)
    }
    
    @objc public func setValueForKey(_ key: String, _ value: NSCoding, handle: @escaping (() -> Void)) {
        guard let cache = self.cacheManage else {
#if DEBUG
            print("保存失败!, 初始化失败")
#endif
            return
        }
        cache.setObject(value, forKey: key, with: handle)
    }
    /**
     * 取
     */
    @objc public func getValueForKey(_ key: String) -> AnyObject? {
        guard let cache = self.cacheManage else {
#if DEBUG
            print("保存失败!, 初始化失败")
#endif
            return nil
        }
        let response = cache.object(forKey: key)
        return response
    }
    /**
     * 删除
     */
    @objc public func deleteValueForKey(_ key: String){
        guard let cache = self.cacheManage else {
#if DEBUG
            print("删除失败!, 初始化失败")
#endif
            return
        }
        let iscontains = self.containsObject(forKey: key)
        if (iscontains){
            cache.removeObject(forKey: key)
        }
    }
    
    /// 删除所有
    public func deleteAllObjects() {
        guard let cache = self.cacheManage else {
#if DEBUG
            print("删除失败!, 初始化失败")
#endif
            return
        }
        cache.removeAllObjects()
    }
    
    /**
     * 是否存在
     */
    @objc public func containsObject(forKey: String) -> Bool {
        guard let cache = self.cacheManage else {
#if DEBUG
            print("获取失败!, 初始化失败")
#endif
            return false
        }
        return cache.containsObject(forKey: forKey)
    }
    
    
}
